package cn.wolfcode.luowowo.wensite.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.exception.LogicException;
import cn.wolfcode.luowowo.member.util.AjaxResult;
import cn.wolfcode.luowowo.wensite.util.UMEditorUploader;
import cn.wolfcode.luowowo.wensite.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("travel")
@Controller
public class TravelController<string> {
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationService destinationService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelCommentService travelCommentService;

    @RequestMapping("")
    public String index(Model model, @ModelAttribute("qo") TravelQuery qo) {
        model.addAttribute("pageInfo", travelService.query(qo));
        return "travel/list";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id) {
        //这里的参数3,表示安装深度来查,1是国家,2是省份,3是城市,我们差的旅游日记是属于城市的
        List<Destination> dests = destinationService.getDestsByDeep(3);
        model.addAttribute("dests", dests);

        if (id != null) {
            Travel detail = travelService.get(id);
            TravelContent content = travelService.getContent(id);
            detail.setTravelContent(content);
            model.addAttribute("tv", detail);
        }
        return "travel/input";
    }

    @Value("${file.path}")
    private String filePath;

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, filePath);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            throw new LogicException("请登录后再操作");
        }
        travel.setAuthor(userInfo);
        Long id = travelService.saveOrUpdate(travel);
        return AjaxResult.SUCCESS.addData(id);
    }

    @RequestMapping("/detail")
    public String detail(Model model, Long id) {
        //detail
        Travel detail = travelService.get(id);
        TravelContent content = travelService.getContent(id);
        detail.setTravelContent(content);
        model.addAttribute("detail", detail);
        //toasts
        List<Destination> toasts = destinationService.getToasts(detail.getDest().getId());
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", destination);
        //sts
        model.addAttribute("sds", strategyDetailService.queryViewNumTop3(detail.getDest().getId()));
        //ts
        model.addAttribute("ts", travelService.queryViewNumTop3(detail.getDest().getId()));


        //查询游记评论列表
        TravelCommentQuery qo = new TravelCommentQuery();
        qo.setTravelId(id);
        qo.setPageSize(Integer.MAX_VALUE);
        Page page = travelCommentService.query(qo);
        model.addAttribute("list", page.getContent());


        return "travel/detail";
    }

    @RequestMapping("/commentAdd")
    public String commentAdd(Model model, TravelComment comment, @UserParam UserInfo userInfo, int floor) {
        comment.setUserId(userInfo.getId());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setCity(userInfo.getCity());
        comment.setUsername(userInfo.getNickname());
        TravelComment travelComment = travelCommentService.saveOrUpdate(comment);
        System.out.println(travelComment.toString());
        //游记评论添加
        model.addAttribute("c",travelCommentService.saveOrUpdate(comment));
        model.addAttribute("floor", travelCommentService.getCount(comment.getTravelId()));
        return "travel/commentTpl";
    }
}
